package com.vilen.weather.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.vilen.weather.model.City;
import com.vilen.weather.model.County;
import com.vilen.weather.model.Province;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vilen on 2015/4/23.
 */
public class WeatherDB {
    public static final String DB_NAME = "vilen_weather";
    public static final int DB_VERSION = 1;
    private static WeatherDB weatherDB;
    private SQLiteDatabase db;
    /**
     * 将构造方法私有化
     */
    private WeatherDB(Context context){
        OpenHelper dbHelper = new OpenHelper(context,DB_NAME,null,DB_VERSION);
        db = dbHelper.getWritableDatabase();
    }
    /**
     * 获取WeatherDb的实例
     * 保证全局范围内只会有一个 CoolWeatherDB 的实例
     */
    public synchronized static WeatherDB getInstance(Context context){
        if(weatherDB == null){
            weatherDB = new WeatherDB(context);
        }
        return weatherDB;
    }
    public void saveProvince(Province province){
        if(province != null) {
            ContentValues values = new ContentValues();
            values.put("province_name",province.getProvinceName());
            values.put("province_code",province.getProvinceCode());
            db.insert("Province",null,values);
        }
    }
    public List<Province> loadProvinces() {
        List<Province> list = new ArrayList<Province>();
        Cursor cursor = db.query("Province",null,null,null,null,null,null);
        if(cursor.moveToFirst()) {
            do {
                Province province = new Province();
                province.setId(cursor.getInt(cursor.getColumnIndex("id")));
                province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
                province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
                list.add(province);
            } while (cursor.moveToNext());
        }
        return list;
    }
    /**
     * City实例存贮到数据库
     */
    public void saveCity(City city) {
        if (city != null) {
            ContentValues values = new ContentValues();
            values.put("city_name",city.getCityName());
            values.put("city_code",city.getCityCode());
            values.put("province_id",city.getProvinceId());
            db.insert("City",null,values);
        }
    }
    public List<City> loadCities(int provinceId) {
        List<City> list = new ArrayList<City>();
        Cursor cursor = db.query("City",null,"province_id = ?",
                new String [] {String.valueOf(provinceId)},null,null,null);
        if(cursor.moveToNext()) {
            do {
                City city = new City();
                city.setId(cursor.getInt(cursor.getColumnIndex("id")));
                city.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
                city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code")));
                city.setProvinceId(provinceId);
                list.add(city);
            } while (cursor.moveToNext());
        }
        return list;
    }
    public void saveCounty(County county){
        if (county != null) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("county_name",county.getCountyName());
            contentValues.put("county_code",county.getCountyCode());
            contentValues.put("city_id",county.getCityId());
            db.insert("County",null,contentValues);
        }
    }
    /**
     * 读取
     */
    public List<County> loadCounties (int city_id){
        List<County> list  = new ArrayList<County>();
        //city_id从数据库中取数据
        Cursor cursor = db.query("County",null,"city_id = ?",new String[]{String.valueOf(city_id)},null,null,null);
        if (cursor.moveToNext()) {
            do {
                County county = new County();
                county.setId(cursor.getInt(cursor.getColumnIndex("id")));
                county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
                county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
                county.setCityId(city_id);
                list.add(county);
            }while (cursor.moveToNext());
        }
        return list;
    }
}
